﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using WorkLogger.Client.DataAccess.Model;

namespace WorkLogger.Client.Controls {
	public partial class LunchTimeView : UserControl, ILunchTimeView {
		private readonly IList<LunchTimeViewItem> items = new List<LunchTimeViewItem>();

		public LunchTimeView() {
			InitializeComponent();
		}

		public event EventHandler AddLunch;
		public event EventHandler EditLunch;
		public event EventHandler RemoveLunch;

		public void SetLunches(IEnumerable<LunchTime> lunches) {
			LunchTime[] lunchArray = lunches
				.OrderBy(lunch => lunch.StartLunch)
				.ToArray();

			if (!CheckDifference(lunchArray))
				return;

			lunchListView.Items.Clear();
			items.Clear();
			foreach (LunchTime lunch in lunchArray) {
				string begin = TimeSpanFormatHelper.FormatDuration(lunch.StartLunch.TimeOfDay);
				var listViewItem = new ListViewItem(begin) { Tag = lunch };
				string end = TimeSpanFormatHelper.FormatDuration(lunch.EndLunch.TimeOfDay);
				listViewItem.SubItems.Add(end);
				listViewItem.SubItems.Add(TimeSpanFormatHelper.FormatDuration(lunch.EndLunch - lunch.StartLunch));
				lunchListView.Items.Add(listViewItem);
				items.Add(new LunchTimeViewItem {
				                                	LunchTimeId = lunch.LunchTimeId,
				                                	StartLunch = lunch.StartLunch,
				                                	EndLunch = lunch.EndLunch
				                                });
			}
		}

		public LunchTime SelectedLunch {
			get {
				if (lunchListView.SelectedItems.Count != 1) {
					return null;
				}
				return (LunchTime)lunchListView.SelectedItems[0].Tag;
			}
		}

		private bool CheckDifference(ICollection<LunchTime> lunches) {
			if (lunches.Count != items.Count) {
				return true;
			}

			return lunches.Where((lunchTime, index) => {
			                     	LunchTimeViewItem item = items[index];
			                     	if (item.LunchTimeId != lunchTime.LunchTimeId)
			                     		return true;
			                     	if (item.StartLunch != lunchTime.StartLunch) {
			                     		return true;
			                     	}
			                     	if (item.EndLunch != lunchTime.EndLunch) {
			                     		return true;
			                     	}
			                     	return false;
			                     }).Any();
		}

		private void LunchContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e) {
			bool lunchSelected = SelectedLunch != null;
			editLunchToolStripMenuItem.Enabled = lunchSelected;
			removeLunchToolStripMenuItem.Enabled = lunchSelected;
		}

		private void AddLunchToolStripMenuItemClick(object sender, EventArgs e) {
			RaiseAddLunch(e);
		}

		private void RaiseAddLunch(EventArgs e) {
			if (AddLunch != null) {
				AddLunch(this, e);
			}
		}

		private void EditLunchToolStripMenuItemClick(object sender, EventArgs e) {
			if (EditLunch != null) {
				EditLunch(this, e);
			}
		}

		private void RemoveLunchToolStripMenuItemClick(object sender, EventArgs e) {
			RaiseRemoveLunch(e);
		}

		private void RaiseRemoveLunch(EventArgs e) {
			if (RemoveLunch != null) {
				RemoveLunch(this, e);
			}
		}

		private void LunchListViewKeyDown(object sender, KeyEventArgs e) {
			if (e.KeyCode == Keys.Insert) {
				RaiseAddLunch(e);
			}
			if (e.KeyCode == Keys.Delete) {
				RaiseRemoveLunch(e);
			}
		}

		private class LunchTimeViewItem {
			public int LunchTimeId { get; set; }
			public DateTime StartLunch { get; set; }
			public DateTime EndLunch { get; set; }
		}
	}
}